import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;

public class FuParser {
	public static String functionized(String linija,String funcn,String funcv) {
		if(!linija.contains(funcn)) {
			return linija;
		}
		int startpos = linija.indexOf(funcn);
		int endpos = 0;
		int count1 = 0;
		int count2 = 0;
		for(int i = startpos;i < linija.length();i++) {
			char c = linija.charAt(i);
			if(c == '(') {
				count1++;
			}
			if(c == ')' && (count1 != count2)) {
				count2++;
			}
			if(c == ')' && (count1 == count2)) {
				endpos = i;
				break;
			}
			
		}
		String funcin = linija.substring(startpos,endpos+1).trim();
		int beginpos1 = funcin.indexOf("(");
		int c1 = 0;
		int c2 = 0;
		int endpos2 = 0;
		for(int i = beginpos1;i < funcin.length();i++) {
			char c = funcin.charAt(i);
			if(c == '(') {
				c1++;
			}
			if(c == ')' && (c1 != c2)) {
				c2++;
			}
			if(c == ')' && (c1 == c2)) {
				endpos2 = i;
				break;
			}
		}
		String num = "("+funcin.substring(beginpos1+1,endpos2)+")";
		funcv = "("+funcv.replace("x",num)+")";
		return linija.replace(funcin,funcv);
	}
	private ArrayList<String> list;
	private ArrayList<String> lines;
	public FuParser(String path) {
		lines = new ArrayList<String>();
		list = new ArrayList<String>();
		ArrayList<String> vars = new ArrayList<String>();
		ArrayList<String> vals = new ArrayList<String>();
		//ArrayList<String> lines = new ArrayList<String>();
		ArrayList<String> funcn = new ArrayList<String>();
		ArrayList<String> funcv = new ArrayList<String>();
		try {
			Scanner ifile = new Scanner(new File(path));
			while(ifile.hasNextLine()) {
				String line = ifile.nextLine();
				line = line.trim();
				if(line.trim().isEmpty()) {
					line = "//newline";
				}
				if(line.length() < 6) {
					line = line+"      ";
				}
				if(line.substring(0,6).compareTo("const ") == 0) {
					String var1 = line.substring(5,line.indexOf("=")-1);
					vars.add(var1.trim().toUpperCase());
					String val1 = "(" + line.substring(line.indexOf("=")+1).trim() + ")";
					vals.add(val1);
				}
				else if(line.substring(0,4).compareTo("def ") == 0) {
					String var1 = line.substring(3,line.indexOf("="));
					funcn.add(var1.trim().toLowerCase());
					String val1 = line.substring(line.indexOf("=")+1).trim();
					funcv.add(val1.trim());
				}
				else if(line.substring(0,6).compareTo("range(") == 0 || line.substring(0,6).compareTo("range ") == 0) {
					String ins = line.substring(line.indexOf("(")+1,line.indexOf(")"));
					String[] ranges = ins.split(",");
					int from = Integer.parseInt(ranges[0].trim());
					int to = Integer.parseInt(ranges[1].trim());
					String function1 = line.substring(line.indexOf(")")+1);
					for(int i = from;i <= to;i++)
					{
						String lini = function1+"("+i+")";
						lines.add(lini.trim());
					}
				}
				else if(line.substring(0,2).compareTo("//") == 0) {
					//nothing
				}
				else {
					line = line.trim();
					lines.add(line);
				}
			}
			ArrayList<String> list1 = new ArrayList<String>();
			for(int j = 0;j < lines.size();j++) {
				String linija = lines.get(j);
				for(int i = 0;i < vars.size();i++) {
					linija = linija.replace(vars.get(i).trim(),vals.get(i));
				}
				list1.add(linija);
			}
			for(int j = 0;j < lines.size();j++) {
				String linija = list1.get(j);
				for(int i = 0;i < funcn.size();i++) {
					if(linija.contains(funcn.get(i))) {
						for(int x = 0;x < linija.length();x++) {
							linija = functionized(linija,funcn.get(i),funcv.get(i));
						}
					}
				}
				list.add(linija);
			}
		}
		catch (FileNotFoundException E) {
			System.out.println(E.toString());
		}
	}
	public ArrayList<String> Lines() {
		return list;
	}
	public ArrayList<String> originalLines() {
		return lines;
	}
}
